package com.glory.wxelectricitypda.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.bean.DefectCodeBean;

import java.util.List;

public class LotDefectDataAdapter extends BaseQuickAdapter<DefectCodeBean, BaseViewHolder> {


    public LotDefectDataAdapter(@Nullable List<DefectCodeBean> data) {
        super(R.layout.item_lot_defect,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, DefectCodeBean item) {
        helper.setIsRecyclable(false);
        helper.setText(R.id.tvDefectCode,item.getACTIONCODE());
        helper.setText(R.id.tvDefectDesc,String.valueOf(item.getACTIONDESC()));
        EditText etDefectQTY = helper.getView(R.id.etDefectQTY);
        ImageView ivDelete = helper.getView(R.id.ivDelete);
        ivDelete.setOnClickListener(view -> {
            remove(helper.getAdapterPosition());
            notifyDataSetChanged();
        });
        setEditListener(etDefectQTY,item);
    }


    private void setEditListener(EditText editText, DefectCodeBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    item.setMAINQTY(s.toString());
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
